package net.zjitc.dao.impl;

import net.zjitc.dao.BrandDao;
import net.zjitc.dao.SingletonJdbcTemplate;
import net.zjitc.models.Brand;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class BrandDaoImpl implements BrandDao {

    @Override
    public List<Brand> find_all() {
        JdbcTemplate jdbcTemplate= SingletonJdbcTemplate.getJdbcTemplate();
        String sql="select * from brands";
        List<Brand> brands= jdbcTemplate.query(sql, new RowMapper<Brand>() {
            @Override
            public Brand mapRow(ResultSet resultSet, int i) throws SQLException {
                Brand brand=new Brand();
                int id=resultSet.getInt("id");
                String  tag=resultSet.getString("tag");
                String  name=resultSet.getString("name");
                brand.setId(id);
                brand.setName(name);
                brand.setTag(tag);
                return brand;
            }
        });
        return brands;
    }

    @Override
    public int create(Brand brand) {
        JdbcTemplate jdbcTemplate= SingletonJdbcTemplate.getJdbcTemplate();
        String sql="insert into brands(name,tag) values(\"adi\",\"sports\")";
        jdbcTemplate.execute(sql);
        return 0;
    }

    @Override
    public int update(Brand brand) {



        return 0;
    }

    @Override
    public int delete(int id) {
        JdbcTemplate jdbcTemplate= SingletonJdbcTemplate.getJdbcTemplate();
        String sql="delete * from brands where id='1'";
        jdbcTemplate.execute(sql);
        return 0;
    }
}
